<?php


namespace App\Http\Controllers\Manage\Customer;


use App\Http\Controllers\Controller;
use App\Http\Requests\Manage\Customer\Index\RejectRequest;
use App\Http\Requests\Manage\Customer\Index\StoreRequest;
use App\Http\Requests\Manage\Customer\Index\UpdateRequest;
use App\Http\Requests\Manage\Customer\Index\UserRequest;
use App\Http\Searchs\Manage\Customer\IndexSearch;
use App\Repositories\Customer;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class IndexController extends Controller
{
    /**
     * @var Customer
     */
    private $customer;

    /**
     * IndexController constructor.
     * @param Customer $customer
     */
    public function __construct(Customer $customer)
    {
        $this->customer = $customer;
    }

    /**
     * @param IndexSearch $search
     * @return JsonResponse
     */
    public function index(IndexSearch $search)
    {
        return $this->response(
            $this->customer->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return JsonResponse
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            DB::transaction(function () use ($request) {
                $this->customer->create(
                    $request->only(['name', 'short_name', 'license', 'remark', 'assure'])
                );
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function edit($id)
    {
        return $this->response($this->customer->find($id));
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($request, $id) {
                $this->customer->update(
                    $id,
                    $request->only(['name', 'short_name', 'license', 'remark', 'assure'])
                );
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function confirm($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->confirm(
                    $id
                );
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function examine($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->examine(
                    $id
                );
            })
        );
    }

    /**
     * @param RejectRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function reject(RejectRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->reject(
                    $id
                );
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function frozen($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->frozen(
                    $id
                );
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function thaw($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->thaw(
                    $id
                );
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function show($id)
    {
        return $this->response(
            $this->customer->show($id)
        );
    }

    /**
     * @param UserRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function user(UserRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($id, $request) {
                $this->customer->user($id, $request->only(['phone', 'name', 'email', 'remark']));
            })
        );
    }

    /**
     * @param $id
     * @return JsonResponse
     */
    public function password($id){
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->password($id);
            })
        );
    }
    /**
     * @param $id
     * @return JsonResponse
     * @throws \Exception
     * @author: luffyzhao@vip.126.com
     * @datetime: 2019/3/27 19:21
     */
    public function destroy($id)
    {
        return $this->response(
            DB::transaction(function () use ($id) {
                $this->customer->delete($id);
            })
        );
    }
}
